function motif_init() 
{
	if (selections == "")
	{
		remove_tool()
		return
	}
	clicked=false
	svgdoc.getElementById("motif_header").firstChild.data = popup_message[23]
	svgdoc.getElementById("motif_ok").firstChild.data = popup_message[24]
	svgdoc.getElementById("motif_renonce").firstChild.data = popup_message[25]
	var objets = selections.substring(0,selections.length - 1).split(";")
	for (var i = 0 ; i < objets.length ; i ++)
	{
		var node = svgdoc.getElementById(objets[i]).cloneNode(true)
		svgdoc.getElementById("MyPattern").appendChild(node)
	}
	var trace = svgdoc.getElementById("lasso")
	svgdoc.getElementById("MyPattern").setAttribute("width", trace.getAttribute("width"))
	svgdoc.getElementById("MyPattern").setAttribute("height", trace.getAttribute("height"))
	svgdoc.getElementById("MyPattern").setAttribute("viewBox", trace.getAttribute("x") + " " + trace.getAttribute("y") + " " + trace.getAttribute("width") + " " + trace.getAttribute("height"))	
	svgdoc.getElementById("motif_size").setAttribute("cx", (15 + parseFloat(trace.getAttribute("width"))).toString())
	svgdoc.getElementById("motif_size").setAttribute("cy", (15 + parseFloat(trace.getAttribute("height"))).toString())
	svgdoc.getElementById("motif_rect").setAttribute("width", trace.getAttribute("width"))
	svgdoc.getElementById("motif_rect").setAttribute("height", trace.getAttribute("height"))
}

function taille_motif(evt)
{
	if (!clicked)
		return
	if (ASV == "6")
	{
		var matrix = svgdoc.getElementById("motif_rect").getScreenCTM().inverse()
		xm = parseInt(evt.clientX * matrix.a + evt.clientY * matrix.c + matrix.e) - 15
		ym = parseInt(evt.clientX * matrix.b + evt.clientY * matrix.d + matrix.f) - 15
	}
	else
	{
		var matrix = svgdoc.getElementById("outil").getCTM().inverse()
		var xm1 = parseInt(pan_x * (1 - 1 / zoom) + coordo_x(evt.clientX) / zoom)
		var ym1 = parseInt(pan_y * (1 - 1 / zoom) + coordo_y(evt.clientY) / zoom)
		xm = xm1 * matrix.a + ym1 * matrix.c + matrix.e - 15
		ym = xm1 * matrix.b + ym1 * matrix.d + matrix.f - 30
	}
	if (xm < 5)
		xm = 5
	if (ym < 5)
		ym = 5
	svgdoc.getElementById("motif_size").setAttribute("cx", (xm + 15).toString())
	svgdoc.getElementById("motif_size").setAttribute("cy", (ym + 15).toString())
	svgdoc.getElementById("MyPattern").setAttribute("width", xm)
	svgdoc.getElementById("MyPattern").setAttribute("height", ym)
	svgdoc.getElementById("motif_rect").setAttribute("width", xm)
	svgdoc.getElementById("motif_rect").setAttribute("height", ym)
}

function outil2_use_prompt(result,data,num_w)
{
	if (!result)
		return
	switch(num_w)
	{
		case 1 : 
		name_file = data
		if (name_file.indexOf("?") == 0)
			name_file = name_file.substring(1 , name_file.length)
		getDataFile("patterns/list_examples.txt",verify_list_patterns)
		break;
	}
}

function verify_list_patterns(data)
{	
if (data.success) 
	if (data.content.indexOf(name_file + ".svg") >=0)
		add_confirm("svg/confirm.svg","outil2",name_file + ".svg " + dialog[36] + "\n" + dialog[37] + "\n" + dialog[38],1)	
	else
	{
		var str = make_pattern()
		remove_tool("outil")
		postURL("php/save_pattern.php?filename=" + name_file + ".svg&overwrite=no",str,callback,"image/xml+svg",null)
	}
else
	add_alert("svg/alert.svg","outil",dialog[18] + "\n" + dialog[19] + "\n" + dialog[20])
}

function outil2_use_confirm(result,num_w)
{
if (!result)
	return
switch(num_w)
{
	case 1 : 
		var str = make_pattern()
		remove_tool("outil")
		postURL("php/save_pattern.php?filename=" + name_file + ".svg&overwrite=yes",str,callback,"image/xml+svg",null)
		break

}
}

function make_pattern()
{
	var nodes = svgdoc.getElementById("MyPattern").childNodes
	for (var i = 0 ; i < nodes.length ; i ++)
		if (nodes.item(i).nodeName == "g")
		{
			nodes.item(i).removeAttribute("id")
			nodes.item(i).removeAttribute("place")
			var childs = nodes.item(i).childNodes
			for (var j = 0 ; j < childs.length ; j ++)
				if (childs.item(j).nodeType == 1)
					childs.item(j).removeAttribute("id")
		}
	var str ="<svg width='220' height='120'>\n"
	str += "<defs>\n"
	str += printNode(svgdoc.getElementById("MyPattern"))
	if (nbpattern > 0)
		for (var i = 1 ; i <= nbpattern ; i++)
		{
			var node = svgdoc.getElementById("paper" + i.toString())
			str += printNode(node) + "\n"
		}
	if (nbgradients > 0)
		for (var i = 1 ; i <= nbgradients ; i++)
		{
			var node = svgdoc.getElementById("grad" + i.toString())
			str += printNode(node) + "\n"
		}
	str += "</defs>\n"
	str += "<rect width='220' height='120' stroke='black' fill='url(#MyPattern)'/>\n"
	str += "</svg>"
	return str
}